/**
 * ------------------------------------------------------------------------------------------------------------------------------------------
 * IE版本判断
 * @returns {string|number}
 * @constructor
 */
function IEVersion() {
	var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
	var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; //判断是否IE<11浏览器
	var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器
	var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf("rv:11.0") > -1;
	if(isIE) {
		var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
		reIE.test(userAgent);
		var fIEVersion = parseFloat(RegExp["$1"]);
		if(fIEVersion == 7) {
			return 7;
		} else if(fIEVersion == 8) {
			return 8;
		} else if(fIEVersion == 9) {
			return 9;
		} else if(fIEVersion == 10) {
			return 10;
		} else {
			return 6;//IE版本<=7
		}
	} else if(isEdge) {
		return 'edge';//edge
	} else if(isIE11) {
		return 11; //IE11
	}else{
		return -1;//不是ie浏览器
	}
}
var IEV = IEVersion();
if(IEV != -1 && IEV != 'edge' && IEV < 10){
	alert("请使用IE10及以上版本浏览器操作，否则部分功能无法正常使用")
}


var element;
var layer;
$(function(){
	layui.use(['layer','element'], function(){
	  	layer = layui.layer; 	// 弹层
	  	element = layui.element;// Tab的切换功能，切换事件监听等，需要依赖element模块
	  	
		leftMeunAdd();
		loadDict();
		
	})  	
})


/**
 * ------------------------------------------------------------------------------------------------------------------------------------------
 * 防止页面后退
 */
/*window.history.go(1);
history.pushState(null, null, document.URL);
window.addEventListener('popstate', function () {
    history.pushState(null, null, document.URL);
});*/

/**
 * ------------------------------------------------------------------------------------------------------------------------------------------
 * TODO
 * 加载所有字典
 */
function loadDict(){
	if(window.sessionStorage) {
		var size = 1024 * 1024 * 5 - unescape(encodeURIComponent(JSON.stringify(sessionStorage))).length;
		if(size < 1000000){ // 如果sessionStorage小于1MB就不存储
			console.log("--------------[sessionStorage]容量过小（kb）--------------: "+size)
			return false;
		}
	}
	$.post(ctxPath+"sys/dict/getDictAll",{},function(res){
		if(res.success){
			var map = res.data['dict_map'];
			var list = res.data['dict_list'];
			sessionStorage.setItem("dict_map", JSON.stringify(map));
			sessionStorage.setItem("dict_list", JSON.stringify(list));
		}
	})
}

/**
 * ------------------------------------------------------------------------------------------------------------------------------------------
 * TODO
 * 左侧菜单添加
 */
function leftMeunAdd(){
	$.post(ctxPath+"sys/menu/selectTree",{},function(res){
		if(res.success){//加载成功了
			$("#left_ul").empty();
			if(themeStyle.indexOf("page1") != -1){
				$("#body_1").css("display","none");
				$("#body_2").css("display","block");
				openTabPage(res.data);
				element.render('nav');
				openLeftMenuItemed(1);
			}else{
				$("#body_1").css("display","block");
				$("#body_2").css("display","none");
				openNotTabPage(res.data);
				element.render('nav');
				openLeftMenuItemed(0);
			}
		}
	})
}

/**
 * ------------------------------------------------------------------------------------------------------------------------------------------
 * 开启标签页
 * @returns
 */
function openTabPage(array){
	for (var i = 0; i<array.length; i++) {
		if (array[i].homeState == 1) {
			currentTabId = array[i].url;
			$("#tab_id").append('<li class="layui-this" lay-id="'+array[i].url+'">'+array[i].mname+'</li>');
			$("#home").attr({"src":array[i].url, "name": array[i].url, "id": array[i].url});
		}
		var li = $("<li class='layui-nav-item'></li>");
		var a = $("<a><i class='"+array[i].iconCls+"'></i><cite>"+array[i].mname+"</cite></a>");
		if(array[i].children.length <= 0){//当没有子菜单是当前可以跳转页面
			li = $("<li class='layui-nav-item clickAddMark'></li>");
			if (array[i].homeState == 1) {
				li = $("<li class='layui-nav-item clickAddMark layui-this'></li>");
			}
			a = $("<a class='a_two' lay-id='"+array[i].url+"' href='javascript:;' lay-direction='2'>" +
					"<i class='"+array[i].iconCls+"' title='"+array[i].mname+"'></i><cite>"+array[i].mname+"</cite></a>");
		}
		$(li).append(a);
		if(array[i].children.length > 0){//存在子菜单
			getMenu1(array[i].children, li, 20);
		}
		$("#left_ul").append(li);
	}
}

/**
 * ------------------------------------------------------------------------------------------------------------------------------------------
 * 递归子菜单
 */
function getMenu1(list,li,left){
	var dl = $("<dl class='layui-nav-child'></div>");
	for (var y = 0; y < list.length; y++) {
		var childList = list[y].children;
		if (list[y].homeState == 1) {
			currentTabId = list[y].url;
			$("#tab_id").append('<li class="layui-this" lay-id="'+list[y].url+'">'+list[y].mname+'</li>');
			$("#home").attr({"src":list[y].url, "name": list[y].url, "id": list[y].url});
		}
		var dd = $("<dd class='layui-nav-item'>");
		var a = $("<a><span style='margin-left: "+left+"px;'><i class='"+list[y].iconCls+"'></i><cite>"+list[y].mname+"</cite></span></a>");
		if(childList.length <= 0){
			dd = $("<dd class='clickAddMark'>");
			if (list[y].homeState == 1) {
				dd = $("<dd class='clickAddMark layui-this'>");
			}
			a = $("<a lay-id='"+list[y].url+"' class='a_two'><span style='margin-left: "+left+"px;'>" +
			 		"<i class='"+list[y].iconCls+"'></i><cite>"+list[y].mname+"</cite></span></a>");
		}
		$(dd).append(a);
		if(childList.length > 0){
			getMenu1(childList, dd, (Number(left)+10));
		}
		$(dl).append(dd);
	}
	$(li).append(dl);
}

/**
 * ------------------------------------------------------------------------------------------------------------------------------------------
 * 关闭标签页
 * @returns
 */
function openNotTabPage(array){
	for(var i = 0; i<array.length; i++) {
		if (array[i].homeState == 1) {
			$("#homeUrl").attr("src", array[i].url);
		}
		var li = $("<li class='layui-nav-item'></li>");
		var a = $("<a><i class='"+array[i].iconCls+"'></i><cite>"+array[i].mname+"</cite></a>");
		if(array[i].children.length <= 0){//当没有子菜单是当前可以跳转页面
			li = $("<li class='layui-nav-item clickAddMark'></li>");
			if (array[i].homeState == 1) {
				li = $("<li class='layui-nav-item clickAddMark layui-this'></li>");
			}
			a = $("<a href='javascript:reloadIframe(\""+array[i].url+"\");' lay-id='"+array[i].url+"' class='a_two' lay-direction='2'>" +
					"<i class='"+array[i].iconCls+"' title='"+array[i].mname+"'></i><cite>"+array[i].mname+"</cite></a>");
		}
		$(li).append(a);
		if(array[i].children.length > 0){//存在子菜单
			getMenu2(array[i].children, li, 20);
		}
		$("#left_ul").append(li);
	}
}

/**
 * ------------------------------------------------------------------------------------------------------------------------------------------
 * 递归子菜单
 */
function getMenu2(list,li,left){
	var dl = $("<dl class='layui-nav-child'></div>");
	for(var y = 0; y<list.length; y++) {
		var childList = list[y].children;
		if (list[y].homeState == 1) {
			$("#homeUrl").attr("src", list[y].url);
		}
		var dd = $("<dd class='layui-nav-item'>");
		var a = $("<a><span style='margin-left: "+left+"px;'><i class='"+list[y].iconCls+"'></i><cite>"+list[y].mname+"</cite></span></a>");
		if(childList.length <= 0){
			dd = $("<dd class='clickAddMark'>");
			if (list[y].homeState == 1) {
				dd = $("<dd class='clickAddMark layui-this'>");
			}
			a = $("<a href='javascript:reloadIframe(\""+list[y].url+"\");' lay-id='"+list[y].url+"' class='a_two'><span style='margin-left: "+left+"px;'>" +
			 		"<i class='"+list[y].iconCls+"'></i><cite>"+list[y].mname+"</cite></span></a>");
		}
		$(dd).append(a);
		if(childList.length > 0){
			getMenu2(childList, dd, (Number(left)+10));
		}
		$(dl).append(dd);
	}
	$(li).append(dl);
}

/**
 * ------------------------------------------------------------------------------------------------------------------------------------------
 * 主体界面跳转
 */
function reloadIframe(url) {
	$("#homeUrl").attr("src", url)
}

/**
 * ------------------------------------------------------------------------------------------------------------------------------------------
 * 展开左侧指定父级菜单
 */
function openLeftMenuItemed(value){
	var count = 0;
	var obj = $('.clickAddMark');
	obj.each(function(){
		var cls = $(this).attr("class");
		if(cls.indexOf('layui-this') != -1){
			count ++;
			var parentTab = $(this).parents(".layui-nav-item");
			$.each(parentTab, function(i) {
				$(this).addClass("layui-nav-itemed");
			})
		}
	})
	if(count == 0 && obj.length > 0){
		$(obj[0]).addClass("layui-this");
		currentTabId = $(obj[0]).find("a").attr("lay-id");
		var name = $(obj[0]).find("cite").text();
		if(value == 1){
			$("#tab_id").append('<li class="layui-this" lay-id="'+currentTabId+'">'+name+'</li>');
			$("#home").attr({"src":currentTabId, "name": currentTabId, "id": currentTabId});
		}else{
			$("#homeUrl").attr("src", currentTabId);
		}
		openLeftMenuItemed(value);
	}
}




